package com.kgc.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.cache.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import com.kgc.dao.BillMapper;
import com.kgc.pojo.Bill;
import com.kgc.service.BillService;

@Service
public class BillServiceImpl implements BillService{

	@Autowired
	private BillMapper billMapper;


	@Cacheable(value = "billCache",condition = "#params.get('start')<20")
	public List<Bill> getBillListPage(Map<String, Object> params) {
		return billMapper.getBillListPage(params);
	}


    @Cacheable(key="'billlistcount'" ,value = "billCache")
	public Integer getBillListPageCount(Map<String, Object> params) {
		return billMapper.getBillListPageCount(params);
	}

    //清缓存
    @Caching(evict = {
            @CacheEvict(key="'billlistbyPage'" ,value = "billCache"),
            @CacheEvict(key="'billlistcount'" ,value = "billCache")
    })
    public Bill addBill(Bill bill) {
		if(billMapper.addBill(bill)>0){
			return  bill;
		}else{
			return null;
		}
	}
    @Caching(evict = {
            @CacheEvict(key="'billlistbyPage'" ,value = "billCache"),
            @CacheEvict(key="'billlistcount'" ,value = "billCache"),
            @CacheEvict(key="#id",value = "billCache")
    })
	public int deleteBill(Integer id) {
		return billMapper.deleteBill(id);
	}
    @Caching(evict = {
            @CacheEvict(key="'billlistbyPage'" ,value = "billCache"),
            @CacheEvict(key="#id",value = "billCache")
    })
	public Bill updateBill(Bill bill) {
		if(billMapper.updateBill(bill)>0){
			return  bill;
		};
		return null;
	}
    @Cacheable(key="#id",value = "billCache")
	public Bill getBillById(Integer id) {
		return billMapper.getBillById(id);
	}

}
